package com.yyn.Learning.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyn.Learning.dto.BaseMessageDto;
import com.yyn.Learning.dto.CoursePublish;
import com.yyn.Learning.dto.front.CourseQueryDto;
import com.yyn.Learning.entity.EduCourse;
import com.yyn.Learning.service.EduCourseService;
import com.yyn.Learning.service.EduSubjectService;
import com.yyn.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2022-11-19
 */
@RestController
@CrossOrigin
@RequestMapping("/Learning/edu-course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduSubjectService eduSubjectService;

    @ApiOperation("保存数据")
    @PostMapping("base")
    public R BaseMessage(@RequestBody BaseMessageDto baseMessageDto) {
        eduCourseService.saveBase(baseMessageDto);
        return R.success().data("courseId", baseMessageDto.getId());
    }

    @ApiOperation("通过id回显数据")
    @GetMapping("base/{id}")
    public R getInfoById(@PathVariable String id) {
        BaseMessageDto infoById = eduCourseService.getInfoById(id);
        return R.success().data("infoById", infoById);
    }

    @ApiOperation("修改info")
    @PostMapping("updateBase")
    public R updateInfo(@RequestBody BaseMessageDto baseMessageDto) {
        eduCourseService.updateInfo(baseMessageDto);
        return R.success().message("修改成功");
    }

    @ApiOperation("最终发布回显信息")
    @GetMapping("publish/{id}")
    public R getCoursePublish(@PathVariable String id) {
        CoursePublish coursePublish = eduCourseService.getCoursePublish(id);
        return R.success().data("coursePublish", coursePublish);
    }

    @ApiOperation("最终发布 修改为已发布状态")
    @PostMapping("publish2/{id}")
    public R updateCourseStatus(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.success().message("发布成功");
    }

//    课程管理列表
    @ApiOperation("课程管理列表")
    @PostMapping("/{page}/{pageSize}")
    public R getCourseList(@PathVariable int page,@PathVariable int pageSize,@RequestBody EduCourse eduCourse){
        Page<EduCourse> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<EduCourse> eduCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduCourseLambdaQueryWrapper.like(StringUtils.isNotEmpty(eduCourse.getTitle()),EduCourse::getTitle,eduCourse.getTitle());
        eduCourseLambdaQueryWrapper.like(StringUtils.isNotEmpty(eduCourse.getStatus()),EduCourse::getStatus,eduCourse.getStatus());
        eduCourseLambdaQueryWrapper.orderByDesc(EduCourse::getGmtCreate);
        Page<EduCourse> page2 = eduCourseService.page(page1, eduCourseLambdaQueryWrapper);
        return R.success().data("courseList",page2);
    }
    @ApiOperation("删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourseById(courseId);
        return R.success();
    }
    //客户端 显示该老师的主要课程
    @ApiOperation("通过老师显示课程")
    @GetMapping("getCourseByTeacherId/{teacherId}")
    public R getCourseByTeacherId(@PathVariable String teacherId){
        List<EduCourse> courseByTeacherId = eduCourseService.getCourseByTeacherId(teacherId);
        return R.success().data("CourseByTeacher",courseByTeacherId);
    }
    @ApiOperation("通过条件分页查询")
    @PostMapping("/front/{page}/{limit}")
    public R pageList(@PathVariable int page,@PathVariable Long limit, @RequestBody(required = false) CourseQueryDto courseQueryDto){
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);//通过泛型已经能获得到page的对象值了  已经有值了
        Map<String, Object> pageListFront = eduCourseService.getPageListFront(eduCoursePage, courseQueryDto);
        return R.success().data(pageListFront);
    }
}

